package com.lagou.mall.esflow.core.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.clubfactory.cat.integration.mybatis.CatMybatisPlugin;
import javax.annotation.Resource;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@SuppressWarnings("SpringJavaAutoWiringInspection")
@Configuration
@MapperScan(value = "com.wholee.account.finance.core.dao.**", sqlSessionFactoryRef = "sqlSessionFactory")
@EnableTransactionManagement
@Slf4j
public class MybatisConfig {

    @Resource
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactory(
            @Qualifier("mybatisPlusInterceptor") MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {
        log.info("------------- 重载父类 sqlSessionFactory init-------");
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        //手动添加cat拦截器
        CatMybatisPlugin catMybatisPlugin = new CatMybatisPlugin();
        factory.setPlugins(mybatisPlusInterceptor, catMybatisPlugin);
        factory.setDataSource(dataSource);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setMapperLocations(resolver.getResources("classpath:/mapper/*Mapper.xml"));

        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        //驼峰
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        // SQL打印方便排查，线上建议关闭
        mybatisConfiguration.setLogImpl(StdOutImpl.class);
        factory.setConfiguration(mybatisConfiguration);
        return factory.getObject();
    }

    @Bean(name = "accountTransactionManager")
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        //防止全表更新
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        //SQL规范
        //interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
        return interceptor;
    }
}
